<?php
namespace app\api\controller;
use app\index\controller\PaymentController;
/**
 * 基本API
 */

class IndexController extends BaseController {

	/**
	 * API信息
	 */
	public function index() {
		$data['ads'] = db('advertise')->select();
		$data['list']= db('goods')->field('id,goods_sn,goods_name,store,sale_price,thumb')->limit(10)->select();
		return $this->toData($data);
	}
	public function send_hb(){
		$ticket_config =get_config('ticket_config');
		$list2=db('member')->where("is_rz",1)->where("rz_type",1)->where("add_time < '2019-01-08' ")->field("id,open_id,is_qy")->select();
		foreach($list2 as $v){
			$type=1;
			if($v['is_qy']==1){
				$type=2;
				$nums2=$ticket_config['qy_days'];//会员发放天数
				$price2=$ticket_config['qy_send'];//会员每天发放
			}else{
				$nums2=$ticket_config['m_days'];//会员发放天数
				$price2=$ticket_config['m_send'];//会员每天发放
			}
			$w['user_id']=$v['id'];
			$w['type']=$type;
			$count=db("hb_list")->where($w)->count();
			dump("id:".$v['id']."price:".$price2);
			if($count<$nums2){
				$log=db('hb_list')->wheretime("add_time",'today')->where($w)->find();
				if(!$log){
					$payment=new PaymentController();
					$res=$payment->add_hbjl($v['id'],$type,$price2);
					if($res){
						$payment->send_pay($v['open_id'],'',$price2,'奖励金发放');
						//$payment->send_hb($v['open_id'],$price2,'奖励金发放');
					}
				}
			}
		}
		exit;
	}
	/**
	 * 更新APP
	 */
	public function update($version) {
		$data['new_version'] = '0.5.1';
		if (apk_verison_compare($data['new_version'], $version) == 1) {
			$data['update'] = 'Yes';
			$data['apk_file_url'] = 'https://raw.githubusercontent.com/WVector/AppUpdateDemo/master/apk/app-debug.apk';
			$data['update_log'] = '1，添加删除信用卡接口。\r\n2，添加vip认证。\r\n3，区分自定义消费，一个小时不限制。\r\n4，添加放弃任务接口，小时内不生成。\r\n5，消费任务手动生成。';
			$data['target_size'] = '5M';
			$data['new_md5'] = '295687E756F569C7159974DD493489A5';
			$data['constraint'] = true;
			return json($data);
		} else {
			$data2['update'] = 'No';
			return json($data2);
		}
	}

	/**
	 * 登录，md5：密码加密1加密0不加密
	 */
	public function login($mobile, $password, $md5 = 1) {
		if (request()->isPost()) {
			if (!is_numeric($mobile)) {
				return $this->toError('参数错误');
			}
			$where['mobile'] = $mobile;
			if ($md5) {
				$where['password'] = md5_salt($password);
			} else {
				$where['password'] = $password;
			}
			$model = db('member')->where($where)->find();
			if ($model) {
				if ($model['status'] == 2) {
					return $this->toError('您已经被系统禁用，请联系客服解封');
				} else {
					return $this->toData($model);
				}
			} else {
				return $this->toError('用户不存在或密码错误');
			}
		} else {
			return $this->toError('请求拒绝');
		}
	}

	/**
	 * 注册
	 */
	public function regist($mobile, $password, $code, $pid = 0) {
		if (request()->isPost()) {
			if (!is_numeric($mobile) || !is_numeric($code)) {
				return $this->toError('参数错误');
			}
			$data['mobile'] = $mobile;
			$data['password'] = md5_salt($password);
			$validate_result = $this->validate($data, 'Regist');
			if ($validate_result !== true) {
				return $this->toError($validate_result);
			}
			//验证码
			$result = $this->check_code($data['mobile'], $code, '用户注册');
			if ($result != 1) {
				return $this->toError('短信验证码错误!');
			}
			if ($pid) {
				$p_member = db('member')->find($data['pid']);
				if ($p_member) {
					$data['pid'] = $p_member['id'];
				} else {
					return $this->toError('邀请人不存在!');
				}
			}
			$data['add_time'] = now_time();
			$data['update_time'] = now_time();
			$data['login_time'] = now_time();
			$data['login_ip'] = $this->request->ip();
			$result = db('member')->insert($data);
			if ($result) {
				return $this->toSuccess('注册成功');
			} else {
				return $this->toError('注册失败');
			}
		} else {
			return $this->toError('请求拒绝');
		}
	}

	// 找回密码 TODO

	/**
	 * 帮助列表
	 */
	public function help($page) {
		$list = db('sxy')->where('cate_id1', 3)->order('add_time desc')->page($page, 10)->select();
		return $this->toData($list);
	}

	/**
	 * 信息反馈
	 */
	public function feedback($uid, $type, $content) {
		if (request()->isPost()) {
			if (!is_numeric($uid) || !is_numeric($type)) {
				return $this->toError('参数错误');
			}
			$member = db('member')->where('status', 1)->find($uid);
			if (!$member) {
				return $this->toError('用户ID不存在');
			}
			$data['u_id'] = $uid;
			$data['pid'] = $type;
			$data['content'] = $content;
			$data['add_time'] = now_time();
			$result = db('complain')->insert($data);
			if ($result) {
				return $this->toSuccess('反馈成功');
			} else {
				return $this->toError('反馈失败');
			}
		} else {
			return $this->toError('请求拒绝');
		}
	}

	/**
	 * 发送短信：注册
	 */
	public function regist_code($mobile) {
		if (request()->isPost()) {
			return $this->get_code(config('aliyun'), config('aliyun.regist_id'), $mobile, "用户注册");
		} else {
			return $this->toError('请求拒绝');
		}
	}

	/**
	 * 发送短信：忘记密码
	 */
	public function forget_code($mobile) {
		if (request()->isPost()) {
			return $this->get_code(config('aliyun'), config('aliyun.forget_id'), $mobile, "找回密码");
		} else {
			return $this->toError('请求拒绝');
		}
	}

	/**
	 * 发送短信
	 * $mobile 手机号
	 * $template_no 模板编号(数据库主键)，注意不是模板ID
	 */
	private function get_code($config, $template_id, $mobile, $message) {
		$code = rand(1000, 9999);
		$data['mobile'] = $mobile;
		$data['code'] = $code;
		$data['message'] = $message;
		$data['add_time'] = now_time();
		include_once ('././extend/smsapi/smsDemo.php');
		$msg = sendSms($config, $template_id, $mobile, 'code', $code);
		if ($msg->Message == "OK") {
			$data['result'] = '发送成功';
			db('verify_code')->insert($data);
			return $this->toData($code);
		} else {
			$err = $msg->Message;
			$data['result'] = '发送失败 - ' . $err;
			db('verify_code')->insert($data);
			return $this->toError('发送失败');
		}
	}

}
